package com.sctvcloud.qingchuan.ui.utils;

import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import com.ruihang.generalibrary.ui.widget.CustomFontTextView;
import com.sctvcloud.qingchuan.R;

import java.text.MessageFormat;

/**
 * @author suming
 * @date 2020/8/12 16:58
 */
public class ControlMediaViewHelper {
    public static final int AUDIO = 2;
    public static final int VIDEO = 3;

    private ImageView mIcon;
    private CustomFontTextView mText;
    private View mRootView;

    public ControlMediaViewHelper(@NonNull View rootView) {
        mRootView = rootView.findViewById(R.id.rootViewMediaControl);
        mIcon = rootView.findViewById(R.id.iconMediaControl);
        mText = rootView.findViewById(R.id.textMediaControl);
    }

    public ControlMediaViewHelper(
            @NonNull ImageView icon,
            @NonNull CustomFontTextView text,
            @NonNull View rootView) {
        mRootView = rootView;
        mIcon = icon;
        mText = text;
    }

    /**
     * 通过不同类型设置不同样式
     *
     * @param newsType   媒体类型 2:音频 3:视频
     * @param length     时长 "00:00:46" "00:01:03.60"
     * @param visibility 封面图地址，用于判断无封面图时隐藏播放标识
     */
    public void setMedia(int newsType, String length, boolean visibility) {
        switch (newsType) {
            case AUDIO:
                setAudio(length, visibility);
                break;
            case VIDEO:
                setVideo(length, visibility);
                break;
            default:
                if (mRootView != null) {
                    mRootView.setVisibility(View.GONE);
                }
                break;
        }
    }

    /**
     * 正在播放
     *
     * @param currentTime 当前时间 格式 00:00
     */
    public void setPlaying(String currentTime) {
        if (mRootView != null && mIcon != null && mText != null) {
            mRootView.setVisibility(View.VISIBLE);
            mIcon.setImageResource(R.drawable.animation_playing);
            AnimationDrawable drawable = (AnimationDrawable) mIcon.getDrawable();
            if (!drawable.isRunning()) {
                drawable.start();
            }
            if (!TextUtils.isEmpty(currentTime)) {
                mText.setText(currentTime);
            }
            mText.setVisibility(TextUtils.isEmpty(currentTime) ? View.GONE : View.VISIBLE);
        }
    }

    /**
     * @param length 时长
     */
    public void setVideo(String length, boolean visibility) {
        if (mRootView != null && mIcon != null && mText != null) {
            mRootView.setVisibility(visibility ? View.VISIBLE : View.GONE);
            mIcon.setImageResource(R.mipmap.icon_video);
            if (!TextUtils.isEmpty(length)) {
                mText.setText(getTime(length));
            }
            mText.setVisibility(TextUtils.isEmpty(length) ? View.GONE : View.VISIBLE);
        }
    }

    /**
     * @param length 时长
     */
    public void setAudio(String length, boolean visibility) {
        if (mRootView != null && mIcon != null && mText != null) {
            mRootView.setVisibility(visibility ? View.VISIBLE : View.GONE);
            mIcon.setImageResource(R.mipmap.icon_audio);
            if (!TextUtils.isEmpty(length)) {
                mText.setText(getTime(length));
            }
            mText.setVisibility(TextUtils.isEmpty(length) ? View.GONE : View.VISIBLE);
        }
    }

    /**
     * 释放
     */
    public void release() {
        if (mIcon != null) {
            Drawable drawable = mIcon.getDrawable();
            if (drawable instanceof AnimationDrawable) {
                AnimationDrawable animDrawable = (AnimationDrawable) drawable;
                if (animDrawable.isRunning()) {
                    animDrawable.stop();
                }
            }
        }
    }

    private String getTime(String length) {
        try {
            String[] split = length.split(":");
            if (split.length == 3) {
                return MessageFormat.format("{0}:{1}", split[1], split[2].substring(0, 2));
            } else {
                return "";
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }
}
